<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2020/6/24
 * Time: 10:30
 */

namespace app\asmpasync\service;


use Defense\AdWhites;
use Defense\Config;
use Defense\File;
use Defense\Import;
use Defense\Recoverys;
use Defense\ScanCmd;
use Defense\Tprule;
use Defense\TpRules;
use Defense\TpWhites;
use Defense\WhiteContent;
use GPBMetadata\Jzzdefensectl;

class Defense
{
    public $classDescription = '主动防御操作';//模块描述
    public $actionDescription = '';//操作描述


    /**
     * @name 防篡改规则 列表
     * @author wx
     * @date 2020/6/24 10:32
     */
    function TpRules($host_id)
    {
        $modifyRule = db('ips_website_protect')
            ->where('host_id', $host_id)
            ->where('status',1)
            ->select();

        $modifyRuleData = $this->TpRule($modifyRule);

        $server = new TpRules();
        $server->setLists($modifyRuleData);

        return $server;
    }

    /**
     * @name 防篡改规则转化
     * @param $data
     * @author wx
     * @date 2020/6/24 10:42
     */
    protected function TpRule($modifyRule)
    {

        $return = array();

        foreach ($modifyRule as $k => $v){

            $serverRule = new Tprule();

            $serverRule->setDirectory($v['website_path']);
            $serverRule->setDynamic($v['website_path']);
            $mrTmp['directory'] = $v['website_path'];

            if($v['file_protect'] == 1){
                $serverRule->setDynamic(true);
                $serverRule->setStatic(false);
                $serverRule->setOther(false);
            }elseif($v['file_protect'] == 2){

                $serverRule->setDynamic(false);
                $serverRule->setStatic(true);
                $serverRule->setOther(false);

            }elseif($v['file_protect'] == 3){

                $serverRule->setDynamic(true);
                $serverRule->setStatic(true);
                $serverRule->setOther(false);

            }else{
                $serverRule->setDynamic(true);
                $serverRule->setStatic(true);
                $serverRule->setOther(true);
            }

            $serverRule->setCreate($v['create'] == 1 ? true : false);
            $serverRule->setEdit($v['modify'] == 1 ? true : false);
            $serverRule->setDelete($v['del'] == 1 ? true : false);

            $return[] = $serverRule;
        }

        return $return;
    }

    /**
     * @name 防篡改排除
     * @author wx
     * @date 2020/6/24 17:33
     */
    public function TpWhites($host_id)
    {
        $modifyExclueRule = db('host_exclude')->where('host_id', $host_id)
            ->where('illegal_modify',1)->select();

        $data = $this->WhiteContent($modifyExclueRule);

        $server = new TpWhites();
        $server->setLists($data);

        return $server;
    }

    /**
     * @name 防篡改排除规则转化
     * @param $modifyExclueRule
     * @author wx
     * @date 2020/6/24 17:38
     */
    protected function WhiteContent($modifyExclueRule, $type = 'Tp')
    {

        $modifyExclueRuleData = array();

        foreach ($modifyExclueRule as $key => $val){
            $serverRule = new WhiteContent();

            $serverRule->setContent($val['content']);
            if($val['type'] == 1){

                $serverRule->setType('file');

            }elseif($val['type'] == 2){

                $serverRule->setType('directory');

            }elseif($val['type'] == 3){

                $serverRule->setType('process');

            }else{

                $serverRule->setType('extension');
            }

            if($type == 'Tp'){//防篡改

                $serverRule->setDefense(false);
                $serverRule->setTamper(true);

            }else{//主动防御
                $serverRule->setDefense(true);
                $serverRule->setTamper(false);
            }

            $serverRule->setCreate($val['create'] == 1 ? true : false);
            $serverRule->setEdit($val['modify'] == 1 ? true : false);
            $serverRule->setDel($val['del'] == 1 ? true : false);
            $serverRule->setCreate($val['create'] == 1 ? true : false);



            $modifyExclueRuleData[$key] = $serverRule;
        }

        return $modifyExclueRuleData;
    }

    /**
     * @name 主动防御排除
     * @author wx
     * @date 2020/6/24 17:51
     */
    public function AdWhites($host_id){

        $defenseExclueRule = db('host_exclude')->where('host_id', $host_id)
            ->where('auto_defense',1)->select();

        $data = $this->WhiteContent($defenseExclueRule, 'Ad');

        $server = new AdWhites();
        $server->setLists($data);

        return $server;
    }

    /**
     * @name 导入IIS站点
     * @return Import
     * @author wx
     * @date 2020/6/26 10:48
     */
    public function Import()
    {
        $server = new Import();
        $server->setStatus(true);

        return $server;
    }

    /**
     * @name 主动防御配置下发
     * @param $host_id
     * @return Config
     * @author wx
     * @date 2020/6/26 10:56
     */
    public function Config($host_id)
    {
        $configData = db('host_status')->where('host_id', $host_id)->find();

        $levelArray = array(
            1 => 'hight',
            2 => 'mid',
            3 => 'low',
        );

        $boolArray = array(
            0 => false,
            1 => true
        );


        $server = new Config();
        $server->setProcstatus($boolArray[$configData['auto_defense']]);
        $server->setAdstatus($boolArray[$configData['shell_defense']]);
        $server->setAdmode($boolArray[$configData['shell_defense_handle']]);
        $server->setAdlevel($levelArray[$configData['shell_defense_level']]);
        $server->setTpstatus($boolArray[$configData['illegal_modify']]);
        $server->setTpdeformity($boolArray[$configData['file_safe']]);
        $server->setPpstatus($boolArray[$configData['empowerment']]);

        return $server;
    }


    /**
     * @name 恢复文件信息
     * @param $host_id
     * @return File
     * @author wx
     * @date 2020/6/26 11:01
     */
    public function File($list)
    {
        foreach($list as $k => $v){

            $server = new File();
            $server->setFiledata($v['data_package']);
            $server->setFilepath($v['path']);

            $return[$k] = $server;
        }


        return $return;
    }

    /**
     * @name 恢复文件列表
     * @param $host_id
     * @author wx
     * @date 2020/6/26 11:02
     */
    public function Recoverys($host_id, $id)
    {

        $list = db('file_attack')->alias('fa')
            ->join('attack_data ad', 'fa.event_id = ad.event_id && fa.atime = ad.atime')
            ->where('fa.host_id', $host_id)
            ->where('fa.id', $id)
            ->field('fa.path, ad.data_package, fa.event_id, fa.atime')->select();


        $data = $this->File($list);

        $server = new Recoverys();
        $server->setLists($data);

        return $server;
    }

    /**
     * @name 扫描命令
     * @author wx
     * @date 2020/6/26 11:03
     */
    public function ScanCmd($host_id, $status){
        //  0  结束, 1 暂停, 2 开始
        //dump($status);

        $server = new ScanCmd();

        $server->setStatus((int)$status);

        return $server;
    }
}